﻿
using Chain.BLL;
using Chain.Model;
using Chain.Wechat;
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public partial class index : Page
{
    public string Domain = PubFunction.curParameter.strDoMain;
    protected string lineLinkurl = "";
    protected string memID = "0";
    protected Random r = new Random();

    public string WeiXinAppID = PubFunction.curParameter.strWeiXinAppID;
    public string WeiXinShopName = PubFunction.curParameter.strWeiXinShopName;

    protected void Page_Load(object sender, EventArgs e)
    {
        string memWeiXinCard = string.Empty;
        if (base.Request["MemWeiXinCard"] != null)
        {

            memWeiXinCard = base.Request["MemWeiXinCard"].ToString();
        }
        if (!string.IsNullOrEmpty(memWeiXinCard))
        {
            Chain.Model.Mem mem = new Chain.BLL.Mem().GetMemByWeiXinCard(memWeiXinCard);
            if (mem != null)
            {
                base.Response.Cookies.Add(new HttpCookie("uid", mem.MemID.ToString()));
                this.lineLinkurl = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}_uri={1}/ReceptionPage/register.aspx&response_type=code&scope=snsapi_base&state={2}#wechat_redirect", PubFunction.curParameter.strWeiXinAppID, this.Domain, mem.MemID);
            }
            base.Response.Redirect("index.aspx");
        }
        else
        {
            HttpCookie wxMem = base.Request.Cookies["uid"];
            if (wxMem != null)
            {
                if (Regex.IsMatch(wxMem.Value, @"^\d+$"))
                {
                    this.memID = wxMem.Value;
                }
                this.lineLinkurl = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}_uri={1}/ReceptionPage/register.aspx&response_type=code&scope=snsapi_base&state={2}#wechat_redirect", PubFunction.curParameter.strWeiXinAppID, this.Domain, this.memID);
            }
            else
            {
                base.Response.Redirect("login.aspx?code=" + base.Request["code"] ?? "");
            }
            this.txtMemID.Value = this.memID;
        }
        if (base.Request["code"] != null)
        {

            string code = base.Request["code"];
            string appid = System.Web.Configuration.WebConfigurationManager.AppSettings["APPID"];
            HttpRequestHelper post = new HttpRequestHelper();
            string str = post.Reqeust("https://api.weixin.qq.com/sns/oauth2/access_token", string.Format("appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, "cd4088088431d5cd192bc320af0aa0b6", code));
            JObject o = JObject.Parse(str);
            if (o["openid"] != null && o["openid"].ToString() != "")
            {
                Chain.BLL.Mem WXMem = new Chain.BLL.Mem();
                Chain.Model.Mem mem = WXMem.GetModel(Convert.ToInt32(this.memID));
                mem.MemWeiXinCards = o["openid"].ToString();
                WXMem.Update(mem);
            }
        }
    }
}


